<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=375008
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 375008</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=375008">Mozilla Bug 375008</a>
<p id="display"></p>
<div id="content">
  <div id='not-focusable'>
    <!-- Disabled elements -->
    <button hidden disabled>foo</button>
    <input hidden disabled>
    <fieldset hidden disabled>foo</fieldset>
    <select hidden disabled><option>foo</option></select>
    <textarea hidden disabled></textarea>
    <optgroup hidden disabled><option>foo</option></optgroup>
    <option hidden disabled>foo</option>
  </div>

  <div id='focusable'>
    <button hidden>foo</button>
    <input hidden>
    <select hidden><option>foo</option></select>
    <textarea hidden></textarea>

    <!-- Those elements are not focusable by default. -->
    <fieldset tabindex=1 hidden>foo</fieldset>
    <optgroup tabindex=1 hidden><option>foo</option></optgroup>
    <option tabindex=1 hidden>foo</option>
  </div>

  <!-- Hidden elements, they have a frame but focus will go through them. -->
  <div id='hidden' style='visibility: hidden;'>
    <button hidden>foo</button>
    <input hidden>
    <fieldset hidden>foo</fieldset>
    <select hidden><option>foo</option></select>
    <textarea hidden></textarea>
    <optgroup hidden><option>foo</option></optgroup>
    <option hidden>foo</option>
  </div>

  <div>
    <input id='witness'>
  </div>

</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 375008 **/

/*
 * This test is divided in three parts:
 * - cases where focus isn't doable but blur should not happen;
 * - cases where focus is doable;
 * - cases where focus isn't doable but blur should still happen.
 */

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(function() {
  // On Mac, this preference needs to be turned on to be able to focus all the
  // form controls we want to focus.
  SpecialPowers.pushPrefEnv({"set": [[ "accessibility.mouse_focuses_formcontrol", true ]]},
                            runTest);
});

function runTest()
{
  var witness = document.getElementById('witness');
  witness.focus();

  var notFocusableElements = document.getElementById('not-focusable').children;
  for (var i=0; i<notFocusableElements.length; ++i) {
    var element = notFocusableElements[i];
    element.hidden = false;
    synthesizeMouseAtCenter(element, {});
    is(document.activeElement, witness,
       "[" + element.tagName + "] witness should still be focused");

    // Cleanup.
    element.hidden = true;
    witness.focus();
  }

  var focusableElements = document.getElementById('focusable').children;
  for (var i=0; i<focusableElements.length; ++i) {
    var element = focusableElements[i];
    element.hidden = false;
    synthesizeMouseAtCenter(element, {});
    is(document.activeElement, element, "focus should have moved to " + element);

    // Cleanup.
    element.hidden = true;
    witness.focus();
  }

  var hiddenElements = document.getElementById('hidden').children;
  for (var i=0; i<hiddenElements.length; ++i) {
    var element = hiddenElements[i];
    element.hidden = false;
    synthesizeMouseAtCenter(element, {});
    is(document.activeElement, document.body,
       "focus should have moved to the body");

    // Cleanup.
    element.hidden = true;
    witness.focus();
  }

  SimpleTest.finish();
}

</script>
</pre>
</body>
</html>
